package com.sxj.corejava.code08_常用类.内部类;

/**
 * @author 石小俊
 * 2024年07月23日 15:55
 */
public class Test02_局部内部类 {

    public static void main(String[] args) {
        OuterTwo outer = new OuterTwo();
        outer.show();
    }

}

class OuterTwo {
    private String name = "admin";
    private int age;

    public OuterTwo() {
    }

    public OuterTwo(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }


    public void show() {
        String phone = "13812345678";

        class InnerTwo {
            private String name = "alice";
            private String gender;

            public InnerTwo() {
            }

            public InnerTwo(String name, String gender) {
                this.name = name;
                this.gender = gender;
            }

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            public String getGender() {
                return gender;
            }

            public void setGender(String gender) {
                this.gender = gender;
            }

            public void print() {
                System.out.println(name);
                System.out.println(age);
                System.out.println(OuterTwo.this.name);
                // 内部类访问内部类所在的外部类方法中的属性时
                // 只能访问final修饰的属性
                // 在JDK8开始,当内部类访问内部类所在的外部类方法中的属性时
                // 默认为这些属性自动添加final关键字
                System.out.println(phone);
                // 只要存在修改操作,则报错
                // phone = "13912345698";
            }
        }
        System.out.println("*********************外部类方法*********************");
        InnerTwo inner = new InnerTwo();
        // 此处不在内部类中,默认访问的都是外部类属性
        System.out.println(name);
        System.out.println(age);
        inner.print();
        System.out.println(inner.name);
        //phone = "13912345698";
    }
}
